package cn.edu.neusoft.zengweidong707.neufood.fragment;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cn.edu.neusoft.zengweidong707.neufood.R;
import cn.edu.neusoft.zengweidong707.neufood.adapter.CollectAdapter;
import cn.edu.neusoft.zengweidong707.neufood.bean.UserCollectionListBean;
import cn.edu.neusoft.zengweidong707.neufood.listener.ListListener;
import cn.edu.neusoft.zengweidong707.neufood.model.UserModel;


public class Collect2 extends BaseFragment {
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private UserModel model;
    private ListListener<UserCollectionListBean> listener;
    private List<UserCollectionListBean> list;
    private CollectAdapter collectAdapter;

    public Collect2() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setLayout_file(R.layout.fragment_collect2);
        listener = new ListListener<UserCollectionListBean>() {
            @Override
            public void onResponse(List<UserCollectionListBean> userCollectionListBeans) {
                list = userCollectionListBeans;
                collectAdapter = new CollectAdapter(getActivity(), list);
                recyclerView.setAdapter(collectAdapter);
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(getActivity(),"请求失败!",Toast.LENGTH_SHORT).show();
            }
        };
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    @Override
    void initView() {
        recyclerView = (RecyclerView) view.findViewById(R.id.foodCollect);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    void initEvent() {

    }

    @Override
    void initData() {
        int uid = Integer.parseInt(getUserid().toString());
        model = new UserModel();
        model.getAllUserCollection(uid, 1, listener);
        onResume();
    }

    @Override
    public void onResume() {
        super.onResume();
        int uid = Integer.parseInt(getUserid().toString());
        model = new UserModel();
        model.getAllUserCollection(uid, 1, listener);
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
